package id.co.rho.scoring.cid.client.utils;


import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: bungrudi
 * Date: 8/3/11
 * Time: 8:25 PM
 */
public class CommonUtils {
    public static String uppercaseFirstLetter(String s) {
        return s.substring(0, 1).toUpperCase()
                + s.substring(1);
    }

    public static boolean isEmpty(String str) {
        if (str != null) {
            return str.trim().length() == 0;
        }
        return true;
    }

    /**
     * Simply replace a null with 0.
     *
     * @param value
     * @return
     */
    public static BigDecimal getValueOrZero(BigDecimal value) {
        return value != null ? value : BigDecimal.ZERO;
    }

    public static int extractYearFromPeriod(int period) {
        return Integer.valueOf(String.valueOf(period).substring(0, 4));
    }

    public static int extractPeriodStringToYear(String period) {
        return Integer.valueOf(String.valueOf(period).substring(0, 4));
    }

    public static boolean equals(Object object1, Object object2) {
        if (object1 == object2) {
            return true;
        }
        if ((object1 == null) || (object2 == null)) {
            return false;
        }
        return object1.equals(object2);

    }

    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;

    }

    public static String append(Collection<String> fontStyles, String appender) {
        StringBuilder sb = new StringBuilder();
        boolean first=true;
        for (String fontStyle : fontStyles) {
            if(!first) {
                sb.append(appender);
            } else {
                first = false;
            }
            sb.append(fontStyle);
        }
        return sb.toString();
    }

    public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
        for (Map.Entry<T, E> entry : map.entrySet()) {
            if (value.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null;
    }
}
